// components/canvas/canvas.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    width:0,
    height:0,
    imagepath:'',
    imgSrc:''

  },

  /**
   * 组件的方法列表
   */
  methods: {async newshare(){
    this.setShare(()=>{
     this.img=wx.createImage();
     img=wx.canvasToTempFilePath({
       canvas: this.canvas,
       destWidth: 375,
       destHeight: 1000,
       success(res) {
         // console.log(res.tempFilePath)
       }
     })
   })
   console.log('====');
 },
async setShare(){
 const query = wx.createSelectorQuery()
 // console.log(query.select('#myCanvas'));
 query
   .select('#myCanvas')
   .fields({ node: true, size: true })
   .exec(res => {
     const canvas = res[0].node
     this.canvas = canvas
     const canvasDpr = wx.getSystemInfoSync().pixelRatio
     const canvasWidth = res[0].width
     const canvasHeight = res[0].height
     const ctx = canvas.getContext('2d')
     console.log(canvasWidth,canvasHeight,ctx);
     const titleimage=canvas.createImage();
     titleimage.src='../../images/武藏.jpg';
     const footimage=canvas.createImage();
     footimage.src='../../images/Cz6Ad5beqq.png';
     const qrcodeimg=canvas.createImage();
     qrcodeimg.src=this.qrcodesrc;
     console.log(canvasWidth,canvasHeight,titleimage,footimage,qrcodeimg);
     ctx.fillText("wuzang",200,100);
     // ctx.drawImage(titleimage,0,0,1960,626,0,0,800,100);
     ctx.drawImage(footimage,0,0,1125*this.pixelRatio,435*this.pixelRatio);
     // ctx.drawImage(qrcodeimg,0,0,200,200,960,200,200,200);
         //然后生成一个保存用的临时地址, 因为安卓机兼容问题, 所以方法要延迟.
            wx.canvasToTempFilePath({
                canvas:this.canvas,
                x: 0,
                y: 0,
                quality:1,
                width:this.data.width,
                height:this.data.height,
                destWidth: this.data.width,
                destHeight: this.data.height,
                success: res => {
                     let path= res.tempFilePath//获取到了临时地址
                    //保存图片
                    this.setData({
                     imagepath:path
                    })
                    console.log(path)
                    wx.saveImageToPhotosAlbum({
                       filePath: path,
                       success: () => {
                         wx.showToast({
                           title: '保存成功',
                           icon: 'none'
                         })
                       }
                    });
       
                },
                fail:(err)=>{
                  console.log(err);
                }
             },this);
   })
 },
 /**
  * 生命周期函数--监听页面加载
  */
 async onLoad(options) {
   wx.getSystemInfo({
     success:(res)=> {
       // 屏幕宽高各乘2倍, 宽高保存到data, 供html用(如果你的图片宽高是固定的,直接在data设置你的图片宽高*2即可)
       this.setData({width: res.windowWidth*res.pixelRatio});
       this.setData({height: res.windowHeight*res.pixelRatio});
       this.bili=res.pixelRatio;
     }
   });
   let res= await wx.cloud.callFunction({
     name:'qrcode'
   })
   this.qrcode=res.result.buffer.data;
   let base=wx.arrayBufferToBase64(this.qrcode);
   this.qrcodesrc='data:image/png;base64,' + base;
   this.setData({
     imgSrc: 'data:image/png;base64,' + base
 })
   // console.log(base);
   // console.log(res);
 },

  }
})
